public class Solution {
    int ret = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        ret = 1;
        maxDepth(root);
        return ret - 1;
    }
    private int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        } else {
            int leftHeight = maxDepth(root.left);
            int rightHeight = maxDepth(root.right);
            ret = Math.max(ret, leftHeight + rightHeight + 1);
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
